<?php

namespace app\weixin\controller;
use think\Controller;
use EasyWeChat\Foundation\Application;
use app\weixin\model\Admins;

class  Index extends Controller
{
   public function index()
   {   
       // config配置
       $options=[
           'debug'  => true,
            'app_id' => appid,
            'secret' => appsecret,
            'token'  => token,
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ],
       ];

     
    
       $url = domain;

       $app = new Application($options);
       $server = $app->server;
       // 消息回复
       $server->setMessageHandler(function ($message) {
           // $message->FromUserName // 用户的 openid
           // $message->MsgType // 消息类型：event, text....

           switch ($message->EventKey) {
             case 'qq':
               return 'qq:2202055656';
               break;
             case '价格':
               return '价格：￥4999';
               break;
             case '官网':
               return '请访问公司网址:<a href="https://www.wolive.cc">WoLive官网</a>';
               break;
             }
             return "您好！欢迎关注我!";

       });

       // 自定义菜单
       $menu = $app->menu;
       $buttons =[
         
            [
                "name"=>"客服系统", 
               "sub_button"=>[ 
                  [
                  "type"=>"view", 
                  "name"=>"绑定/解绑", 
                  "url"=>$url."/weixin/login/callback"
                  ], 
                  [ 
                  "type"=>"view", 
                  "name"=>"工作台", 
                  "url"=>$url."/weixin/chat"
                  ], 
                 ],
            ], 
       ];


       $menu->add($buttons);
       $response = $server->serve();
       $response->send();
   }

}
